/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package com.android1.amarena2d.texture;

import com.android1.amarena2d.engine.EngineObject;
import com.android1.amarena2d.engine.Resource;

public class ManagedBitmapFont extends EngineObject implements Resource, Resource.Consumer {

    protected final Resource.Manager resourceManager = new Resource.Manager(this);

    boolean init;

    BitmapFontDef def;
    BitmapFont bitmapFont;

    public ManagedBitmapFont(BitmapFontDef def) {
        this.def = def;
    }

    public ManagedBitmapFont() {
    }

    public void init() {
        if (!init) {
            if (def != null) {
                def.managedTexture.obtainRef(this);
                def.managedTexture.init();
                bitmapFont = new BitmapFont(engine.getFiles().getFileHandle(def.fontfilepath, def.fontfileType), def.managedTexture.getTexture(), def.flip);
            } else {
                bitmapFont = new BitmapFont();
            }
            this.init = true;
        }
    }

    public void dispose() {
        if (init && resourceManager.canDispose()) {
            init = false;
            if (def != null) {
                def.managedTexture.releaseRef(this);
                def.managedTexture.dispose();
                //no bitmapFont.dispose() cause this would kill the texture
            } else {
                bitmapFont.dispose();
            }

        }
    }

    public BitmapFont getBitmapFont() {
        return bitmapFont;
    }


    @Override
    public void obtainRef(Consumer consumer) {
        resourceManager.obtainRef(consumer);
    }

    @Override
    public void releaseRef(Consumer consumer) {
        resourceManager.releaseRef(consumer);
    }

    @Override
    public void releaseAllRefs() {
        resourceManager.releaseAllRefs();
    }

    @Override
    public boolean hasRefs() {
        return resourceManager.hasRefs();
    }

    @Override
    public boolean isEagerDispose() {
        return resourceManager.isEagerDispose();
    }

    @Override
    public void setEagerDispose(boolean dispose) {
        resourceManager.setEagerDispose(dispose);
    }


    @Override
    public boolean equalResource(Resource otherResource) {
        return this == otherResource;
    }

    @Override
    public void markPersistent() {
        resourceManager.markPersistent();
    }

    @Override
    public void revokePersistent() {
        resourceManager.revokePersistent();
    }


    @Override
    public void register(StateListener listener) {
        this.resourceManager.register(listener);
    }

    @Override
    public void unregister(StateListener listener) {
        this.resourceManager.unregister(listener);
    }


    public String getKey() {
        if (def != null)
            return def.key;
        else return null;
    }

    public boolean isInit() {
        return init;
    }
}